﻿package zf.media.audio.spectrum
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import zf.media.audio.SoundSpectrumMode;

	/**
	 * 光滑直线图谱图形 
	 * @author mike.zhang
	 * 
	 */
	public class SmoothLineSpectrum extends SoundSpectrum
	{
		private var lineSprite:Sprite;
		private var jd:Number = 0;
		private var randomCtArr:Array;
		
		private var pa:Array;
		private var pab:Array;
		private var xinv:Number;
		
		private var gradientBoxMatrix:Matrix
		/**
		 * 创建光滑直线图谱图形 
		 * @param w 宽度
		 * @param h 高度
		 * 
		 */
		public function SmoothLineSpectrum (w:Number,h:Number):void
		{
			super (w,h);
			type=SoundSpectrumMode.SLS;
			randomCtArr=new Array;
			for(var i:uint=0;i<6;i++)
			{
				randomCtArr[i]=getCt().color;
			}
			ff=16;
			lineSprite = new Sprite();
			pa=new Array;
			pab=new Array;
			xinv=w/ff;
			
			gradientBoxMatrix=new Matrix;
			gradientBoxMatrix.createGradientBox (ww,hh,0,0,0);
			//lineSprite.graphics.lineStyle (1);
			//lineSprite.graphics.lineGradientStyle(GradientType.LINEAR, [randomCtArr[0],randomCtArr[1], randomCtArr[2]], [1, 1, 1], [0, 64, 128], gradientBoxMatrix);

		}
		/**
		 * @private 
		 * 
		 */
		override protected function draw ():void
		{
			super.draw ();

			var l:uint = byArr.length;
			lineSprite.graphics.clear ();

			var i:int=0;
			var xx:Number;
			var yy:Number;
		
			for (i=0; i<ff*0.5; i++)
			{
				xx=(ww+10)*i*2/l+xinv;
				yy=-byArr[i]*hh*0.4 + Math.cos(jd)*(hh*0.125)+hh*0.5;
				pa[i]=new Point(xx,yy);
			}
			for (i=ff*0.5; i<ff; i++)
			{
				xx=(ww+10)*(i-l*0.5)*2/l+xinv;
				yy=byArr[i]*hh*0.4 - Math.cos(jd)*(hh*0.125)+hh*0.5;
				pa[i]=new Point(xx,yy);
			}
			for (i=1; i<ff*0.5; i++)
			{
				pab[i-1]=new Point((pa[i].x+pa[i-1].x)*0.5,(pa[i].y+pa[i-1].y)*0.5);
			}
			for (i=ff*0.5+1; i<ff; i++)
			{
				pab[i-1]=new Point((pa[i].x+pa[i-1].x)*0.5,(pa[i].y+pa[i-1].y)*0.5);
			}
			lineSprite.graphics.moveTo (pa[0].x,pa[0].y);
			lineSprite.graphics.lineStyle (2);
			lineSprite.graphics.lineGradientStyle(GradientType.LINEAR, [randomCtArr[0],randomCtArr[1], randomCtArr[2]], [1, 1, 1], [0, 128, 255], gradientBoxMatrix);
			for (i=1; i<ff*0.5-1; i++)
			{
				lineSprite.graphics.curveTo (pa[i].x, pa[i].y,pab[i].x,pab[i].y);
			}
			lineSprite.graphics.moveTo (pa[int(ff*0.5)].x,pa[int(ff*0.5)].y);
			lineSprite.graphics.lineStyle (2);
			lineSprite.graphics.lineGradientStyle(GradientType.LINEAR, [randomCtArr[3],randomCtArr[4], randomCtArr[5]], [1, 1, 1], [0, 128, 255], gradientBoxMatrix);
			for (i=ff*0.5+1; i<ff-1; i++)
			{
				lineSprite.graphics.curveTo (pa[i].x, pa[i].y,pab[i].x,pab[i].y);
			}

			jd += 0.01;
			
			//lineSprite.filters=new Array(new GlowFilter(randomCt.color,1,2,2,4));
			_bg.bitmapData.draw (lineSprite,null,null,_blendMode);//将效果容器中的图像复制
			_bg.bitmapData.applyFilter (_bg.bitmapData, _bg.bitmapData.rect, new Point(), new BlurFilter(4, 4, 1));//应用模糊滤镜
			_bg.bitmapData.colorTransform (_bg.bitmapData.rect, new ColorTransform(1, 1, 1, 1, -5, -10, -10));//进行颜色转换，产生火焰颜色
			_bg.bitmapData.scroll (1,0);
		}
		
		override public function defaultConfig():void
		{
			abs=false;
			machining=false;
		}
	}
}